﻿using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
using DG.Tweening;
using UnityEngine.UI;


public enum BtnType
{
    STATIC,
    BREATHE,
    ANIM,
}

public enum BtnState
{
    Nomal,
    Disabled,
}

public class ButtonAnimation : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler
{
    public static float scaleTime1 = 0.5f;
    public static float scaleTime2 = 0.7f;
    [SerializeField]
    private BtnType btntype = BtnType.STATIC;

    [SerializeField]
    private BtnState btnstate = BtnState.Nomal;

    [SerializeField]
    private Image glowImg = null;

        
    private string AnimationStaticName;

    //public string audioName = "button";

    [SerializeField]
    public UnityEvent m_OnClick = new UnityEvent();


    private bool isCallBack = false;
    private bool isPointDown = false;

    private bool isPointDownCheck = false;
    private float lastInvokeTime;
    private float UpBtnTime = 0.1f;


    void Awake()
    {
        SetType(btntype); 
    }


    void ReceviceButtonScale(params object[] objs)
    {
        //Debug.Log("ButtonAnimation::ReceviceButtonScale()");
        if (btntype != BtnType.BREATHE && btntype != BtnType.ANIM) return;
        if (glowImg != null && !glowImg.gameObject.activeSelf)
            glowImg.gameObject.SetActive(true);

        if (btnstate != BtnState.Nomal)
            return;


        ButtonPingpong();
    }

    public void SetType(BtnType btntype, string animName = "")
    {
        //Debug.Log("ButtonAnimation::SetType()");
        AnimationStaticName = animName;
        if (this.btntype == btntype)
            return;
        this.btntype = btntype;
        if (btntype == BtnType.BREATHE)
        {
            //抬起时间
            UpBtnTime = 0.03f;
            gameObject.transform.DOScale(new Vector3(1, 1, 1), 0.01f);
            if (glowImg != null)
            {
                glowImg.gameObject.SetActive(true);
                glowImg.DOFade(0, 0.01f);
            }
        }
        else
        {
            //抬起时间
            UpBtnTime = 0.045f;
            if (glowImg != null)
                glowImg.gameObject.SetActive(false);
        }

    }

    public void ChangeStates(BtnState state)
    {
        //Debug.Log("ButtonAnimation::ChangeStates()");
        btnstate = state;
    }

    /// <summary>
    /// 按钮待机状态
    /// </summary>
    /// <param name="obj"></param>
    void ButtonPingpong()
    {
        //Debug.Log("ButtonAnimation::ButtonPingpong()");
        if (glowImg != null)
            glowImg.DOFade(1, scaleTime1).SetEase(Ease.InSine);
        if (glowImg != null)
            glowImg.DOFade(0, scaleTime2).SetEase(Ease.OutQuad).SetDelay(scaleTime1);

        if (isPointDown) return;
        if (isPointDownCheck) return;

        gameObject.transform.DOScale(new Vector3(1.04f, 1.04f, 1), scaleTime1).SetEase(Ease.InSine);

        gameObject.transform.DOScale(new Vector3(0.97f, 0.97f, 1), scaleTime2).SetEase(Ease.OutQuad).SetDelay(scaleTime1);

    }
    public void OnPointerDown(PointerEventData eventData)
    {
        //Debug.Log("ButtonAnimation::OnPointerDown()");
        if (btnstate != BtnState.Nomal)
        {
            isCallBack = false;
            return;
        } 
        //Debug.Log("OnPointerDown");
        if (isPointDown) return;
        if (isPointDownCheck) return;


        isCallBack = true;

        isPointDownCheck = true;
        isPointDown = true;
        lastInvokeTime = Time.time;

        if (btntype == BtnType.BREATHE)
        {
            gameObject.transform.DOKill();
            gameObject.transform.DOScale(new Vector3(0.9f, 0.9f, 1), 0.1f);
        }
        else
        {
            gameObject.transform.DOKill();
            gameObject.transform.DOScale(new Vector3(0.9f, 0.9f, 1), 0.1f);
        }
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        //Debug.Log("ButtonAnimation::OnPointerUp()");
        if (btnstate != BtnState.Nomal)
        {
            isCallBack = false;
            return;
        }
        //Debug.Log("OnPointerUp");
        isPointDown = false;
        if (btntype == BtnType.BREATHE)
        {
            gameObject.transform.DOScale(new Vector3(1, 1, 1), UpBtnTime).OnComplete(() => ButtonPingpong());
        }
        else
        {
            gameObject.transform.DOScale(new Vector3(1, 1, 1), UpBtnTime);
        }

        gameObject.transform.DORotate(Vector3.zero, UpBtnTime).OnComplete(() =>
        {
            isPointDown = false;
            isPointDownCheck = false;
        });
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        //Debug.Log("ButtonAnimation::OnPointerExit()");
        if (btnstate != BtnState.Nomal)
        {
            isCallBack = false;
            return;
        }
        //isPointDownCheck = false;
        //isPointDown = false; 
        //Debug.Log("OnPointerExit");

    }
    public void OnPointerClick(PointerEventData eventData)
    {
        //Debug.Log("ButtonAnimation::OnPointerClick()");
        if (btnstate != BtnState.Nomal)
        {
            isCallBack = false;
            return;
        }
        //统一播放按钮声音  
        //AudioPlayManager.PlaySFX2D("button");
        //AudioPlayManager.PlaySFX2D(DataAudio.Instance.GetContentByKeyAndType(AudioKey.button, AudioType.name));
        //SoundController.action.playNow("ButtonClick"); 
        // Debug.Log("OnPointerClick      " + UpBtnTime);
        if (isCallBack)
        { 
            gameObject.transform.DOScale(new Vector3(1, 1, 1), UpBtnTime).OnComplete(() =>
            {
                m_OnClick?.Invoke();
            });
        }
   
        isCallBack = false;
    }

}